<template>
  <div class="login-container">
    <div class="header-biaoti">商铺管理后台中心</div>
    <!-- 登录盒子 -->
    <div class="login-box">
      <el-form label-width="60px" class="form-items" :model="loginform" :rules="loginFormRules" ref="loginFormRef">
        <el-form-item label="账号 :" class="form-items-item" prop="user_name">
          <el-input placeholder="请输入账号" class="input" v-model="loginform.user_name"></el-input>
        </el-form-item>
        <el-form-item label="密码 :" class="form-items-item" prop="password">
          <el-input placeholder="请输入密码" class="input" v-model="loginform.password" type="password"></el-input>
        </el-form-item>
        <el-form-item class="form-items-btn">
          <el-button type="primary" @click="login" class="login-in">登录</el-button>
          <el-button @click="loginVisible" class="register">注册</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 注册对话框 -->
    <el-dialog
      title="平台注册"
      :visible.sync="dialogVisible"
      width="50%">
        <el-form label-width="120px" class="form-items" :model="registerform" :rules="registerformRules" ref="registerformRef">
          <el-form-item label="你的账号 :" class="form-items-item" prop="user_name">
            <el-input
              placeholder="请输入账号"
              class="input"
              v-model="registerform.user_name"
            ></el-input>
          </el-form-item>
          <el-form-item label="你的密码 :" class="form-items-item" prop="password">
            <el-input
              placeholder="请输入密码"
              class="input"
              v-model="registerform.password"
              type="password"
            ></el-input>
          </el-form-item>
          <el-form-item label="确认密码 :" class="form-items-item" prop="newspassword">
            <el-input
              placeholder="请确认密码"
              class="input"
              v-model="registerform.newspassword"
              type="password"
            ></el-input>
          </el-form-item>
          <el-form-item class="form-items-btn">
          </el-form-item>
        </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogVisiblelogin">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'login',
  data () {
    return {
      // 这是登录表单绑定数据
      loginform: {
        user_name: 'admin',
        password: 'a123456'
      },
      // 验证规则
      loginFormRef: '',
      // 这个是表单的验证规则
      loginFormRules: {
        // 验证用户名
        user_name: [
          { required: true, message: '请输入账号', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 位字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 16, message: '长度在 6 到 16 位字符', trigger: 'blur' }
        ]
      },
      dialogVisible: false,
      // 注册表单绑定
      registerform: {
        user_name: '',
        password: '',
        newspassword: ''
      },
      registerformRef: '',
      // 注册表单验证码
      registerformRules: {
        // 验证用户名
        user_name: [
          { required: true, message: '请输入账号', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 位字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 16, message: '长度在 6 到 16 位字符', trigger: 'blur' }
        ],
        newspassword: [
          { required: true, message: '请确认密码', trigger: 'blur' },
          { min: 6, max: 16, message: '长度在 6 到 16 位字符', trigger: 'blur' }
        ]
      }
    }
  },
  created () {
  },
  mounted () {

  },
  methods: {
    // 登录
    login () {
      // 验证表单是否通过 合法就通过 不合法就不能通过 validate()Function(callback: Function(boolean, object)) 是一个方法 意思是验证整个表单的方法 而 validateField 对部分表单字段进行校验的方法:
      // Function(props: array | string, callback:
      // Function(errorMessage: string))
      this.$refs.loginFormRef.validate(async valid => {
        // console.log(valid)
        if (!valid) return console.log(!valid)
        // 通过的时候 我们要做一些什么,
        // 响应提交,先post提交表单信息,把响应的数据data的res  在打印res得到一些什么! 要用 一个正确娥账号密码测试一下!
        const { data: res } = await this.$http.post('login', this.loginform)
        // console.log(this.loginform)
        console.log(res)
        // if (res.meta.status !== 400) {
        if (res.meta.status !== 200) {
          return this.$message.error('登录失败')
        } else {
          this.$message.success('登录成功')
          // 登录成功就用路由 $router.push()来还页面
          window.sessionStorage.setItem('token', res.data.token)
          setTimeout(() => {
            this.$router.push('/')
          }, 700)
        }
      })
    },
    // 打开注册框
    loginVisible () {
      this.dialogVisible = true
    },
    dialogVisiblelogin () {
      this.dialogVisible = false
      var value1 = this.registerform.password
      var value2 = this.registerform.newspassword
      if (value1 !== value2) return this.$message.error('你两次输入的密码不相同,请重试!')
      this.$refs.registerformRef.validate(async valid1 => {
        const { data: res } = await this.$http.post('register', {
          user_name: this.registerform.user_name,
          password: this.registerform.password
        })
        // console.log(res)
        if (res.meta.status !== 200) return this.$message.error('注册失败, ' + res.meta.msg)

        this.$message.success('注册成功,请登陆')
        // 注册成功把注册的账号提交的登录表单
        this.loginform = this.registerform
      })
    }
  }
}
</script>

<style scoped lang="less">
.login-container {
  height: 100%;
  background: url(https://img.iabao.com/imgs/2020/10/340b86f86b608343.jpg);
  background-size: cover;
  .header-biaoti {
  text-align: center;
  position: relative;
  top: 120px;
  font-size: 48px;
  // padding: 200px;
  color: #ffffff;
  text-shadow: 0.1em 0.1em #333
  }
  /deep/ .login-box {
    height: 200px;
    width: 360px;
    position: relative;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    .el-form-item__label{
        color: #fff;
    }
    .form-items {
      .form-items-item {
        .el-input__inner{
          background: rgba(0, 0, 0, .3);
          color: #fff;
        }
      }
      padding:20px 5px;
      .form-items-btn {
        display: flex;
        justify-content: center;
        .login-in {
          width: 200px;
          background: rgba(0, 0, 0, .3);
          // color: #000;
          outline: none;
        }
      }
    }
  }
}
/deep/.el-form-item__label {
  color: #000;
}
</style>
